calle = {
	bindEvents: function(){
		$(document).on("click",".btnEdit",function(){
		   	var id = $('td:first', $(this).parents('tr')).text();
	    	var formData = { "id": id };
	    	request = $.ajax({
	    	    url : "DevolverCalle",
	    	    type: "POST",
	    	    data : formData,
	    	});
			
			request.done(function (response, textStatus, jqXHR){
				$("#id_calle").val(id);
				$("#descripcion_calle").val(response.descripcion);
				$(".hdnInput").val("editar");
			});
			
	    });
		
		$(document).on("click",".btnConfirmar",function(){
			if($("#descripcion_calle").val() != "" ){
				var formData = {};
				formData.type = $(".hdnInput").val();
				if($(".hdnInput").val() == "editar"){
					formData.id = $("#id_calle").val();
				}
				
				formData.descripcion = $("#descripcion_calle").val(); 
				
				request = $.ajax({
				    url : "ABMCalle",
		    	    type: "POST",
		    	    data : formData,
		    	});
				request.done(function (response, textStatus, jqXHR){
					$("#id_calle").val("");
					$("#descripcion_calle").val("");
					$(".hdnInput").val("nuevo");
					if(response.action == "editar"){
						var element = $('td#CalleID.'+response.id).parent();
						element.find('td:nth-child(2)').text(response.descripcion);
					}
				});
				
			}else{
				if($("#descripcion_calle").val() == ""){
					calle.mostrarNotificacion("Error: Debe completar los campos del formulario", "top right", "error");
				}
			}
		});
	},
	
	mostrarNotificacion: function(str, position, type){
		$.notify(str, { globalPosition: position,
			  		className: type,
			  		clickToHide: true,
			  		autoHide: true,
			  		autoHideDelay: 4000,
		     	  });
	},
};

$(document).ready(function(){
	$(".mantenimiento").addClass("active");
	calle.bindEvents();
	$('#btnPopover').click(function(){
		$('.form-control').focus();
		$('.account').popover({
	        title: '<span class="popoverTit">Perfil</span>',
	        content: $('.accountHide').html(),
	        placement: 'bottom',
	        html: true
	    });
	});
});